#ifndef _MYCGI_
#define _MYCGI_

#include <stdio.h>

typedef struct
{
  char *name;
  int nvalues;
  char **values;
} CGIVar;

typedef struct
{
  char *varsBuf;
  int nvars;
  CGIVar *vars;
} CGIVars;

typedef struct
{
  char *name;
  char *path;
  char *contentType;
} CGIFile;

typedef struct
{
  char *name;
  int nFiles;
  CGIFile *files;
} CGIFileVar;

typedef struct
{
  char *name;
  char *value;
} CGIHeader;

#define CGI_SECURE_YES 1
#define CGI_SECURE_NO 2

#define CGI_HTTPONLY_YES 1
#define CGI_HTTPONLY_NO 2

typedef struct
{
  int flushed;
  char *name;
  char *version;
  char *value;
  char *comment;
  char *domain;
  char *maxAge;
  char *expires;
  char *path;
  int secure;
  int httpOnly;
} Cookie;

typedef struct
{
  FILE *cgiout;
  int flushed;

  CGIHeader *headerVars;
  int maxHeaderVars;
  int nHeaderVars;

  CGIVars *getVars;
  CGIVars *postVars;

  CGIFileVar *fileVars;
  int maxFileVars;
  int nFileVars;

  Cookie *cookies;
  int maxCookies;
  int nCookies;
} CGI;

CGI *cgiInit();
void cgiDelete(CGI *cgi);

int cgiLoadVars(CGI *cgi);
int cgiLoadCookies(CGI *cgi);

CGIVar *cgiGETVar(CGI *cgi, char *varName);
CGIVar *cgiPOSTVar(CGI *cgi, char *varName);

Cookie *cgiCookie(CGI *cgi, char *name, char *version);
Cookie *cgiSetCookie(CGI *cgi, char *name, char *version, char *value, char *domain, char *path, char *comment, int secure, int httpOnly, char *maxAge, char *expires);
int cgiSetCookieParams(Cookie *cookie, char *version, char *value, char *domain, char *path, char *comment, int secure, int httpOnly, char *maxAge, char *expires);
int cgiClearCookie(Cookie *cookie);
int cgiClearCookieByNameVersion(CGI *cgi, char *name, char *version);

CGIHeader *cgiSetHeader(CGI *cgi, char *name, char *value);

//implementar metodos pela estrutura e ByName
CGIHeader *cgiHeader(CGI *cgi, char *name);
int cgiClearHeader(CGI *cgi, char *name);

void cgiStartFlush(CGI *cgi);
void cgiStopFlush(CGI *cgi);
FILE *cgiGetOut(CGI *cgi);

#define cgiPrintf(cgi, ...) fprintf(cgiGetOut(cgi), __VA_ARGS__)

#define cgiPrintChar(cgi, val) cgiPrintf(cgi,"%c",val)
#define cgiPrintInt(cgi, val) cgiPrintf(cgi,"%d",val)
#define cgiPrintLong(cgi, val) cgiPrintf(cgi,"%li",val)
#define cgiPrintPChar(cgi, val) cgiPrintf(cgi,"%s",val)
#define cgiPrintFloat(cgi, val) cgiPrintf(cgi,"%f",val)
#define cgiPrintHex(cgi, val) cgiPrintf(cgi,"%x",val)

char *cgiEnv(char *envVarName);

#define ENV_DOCUMENT_ROOT "DOCUMENT_ROOT"
#define ENV_GATEWAY_INTERFACE "GATEWAY_INTERFACE"
#define ENV_HTTP_COOKIE "HTTP_COOKIE"
#define ENV_REQUEST_URI "REQUEST_URI"
#define ENV_QUERY_STRING "QUERY_STRING"
#define ENV_CONTENT_LENGTH "CONTENT_LENGTH"
#define ENV_CONTENT_TYPE "CONTENT_TYPE"
#define ENV_REQUEST_METHOD "REQUEST_METHOD"
#define ENV_HTTP_ACCEPT "HTTP_ACCEPT"
#define ENV_HTTP_ACCEPT_CHARSET "HTTP_ACCEPT_CHARSET"
#define ENV_HTTP_ACCEPT_ENCODING "HTTP_ACCEPT_ENCODING"
#define ENV_HTTP_ACCEPT_LANGUAGE "HTTP_ACCEPT_LANGUAGE"
#define ENV_HTTP_CONNECTION "HTTP_CONNECTION"
#define ENV_HTTP_HOST "HTTP_HOST"
#define ENV_HTTP_USER_AGENT "HTTP_USER_AGENT"  
#define ENV_PATH_INFO "PATH_INFO"
#define ENV_PATH_TRANSLATED "PATH_TRANSLATED"
#define ENV_REMOTE_HOST "REMOTE_HOST"
#define ENV_REMOTE_ADDR "REMOTE_ADDR"
#define ENV_REMOTE_PORT "REMOTE_PORT"
#define ENV_REMOTE_USER "REMOTE_USER"
#define ENV_REMOTE_IDENT "REMOTE_IDENT"
#define ENV_AUTH_TYPE "AUTH_TYPE"
#define ENV_SCRIPT_NAME "SCRIPT_NAME"
#define ENV_SCRIPT_FILENAME "SCRIPT_FILENAME"
#define ENV_SERVER_ADDR "SERVER_ADDR"
#define ENV_SERVER_ADMIN "SERVER_ADMIN"
#define ENV_SERVER_NAME "SERVER_NAME"
#define ENV_SERVER_PORT "SERVER_PORT"
#define ENV_SERVER_PROTOCOL "SERVER_PROTOCOL"
#define ENV_SERVER_SIGNATURE "SERVER_SIGNATURE"
#define ENV_SERVER_SOFTWARE "SERVER_SOFTWARE"

#define cgiDocumentRoot() cgiEnv(ENV_DOCUMENT_ROOT)
#define cgiGatewayInterface() cgiEnv(ENV_GATEWAY_INTERFACE)
#define cgiHttpCookie() cgiEnv(ENV_HTTP_COOKIE)
#define cgiRequestURI() cgiEnv(ENV_REQUEST_URI)
#define cgiQueryString() cgiEnv(ENV_QUERY_STRING)
#define cgiContentLength() cgiEnv(ENV_CONTENT_LENGTH)
#define cgiContentType() cgiEnv(ENV_CONTENT_TYPE)
#define cgiRequestMethod() cgiEnv(ENV_REQUEST_METHOD)
#define cgiHttpAccept() cgiEnv(ENV_HTTP_ACCEPT)
#define cgiHttpAcceptCharset() cgiEnv(ENV_HTTP_ACCEPT_CHARSET)
#define cgiHttpAcceptEncoding() cgiEnv(ENV_HTTP_ACCEPT_ENCODING)
#define cgiHttpAcceptLanguage() cgiEnv(ENV_HTTP_ACCEPT_LANGUAGE)
#define cgiHttpConnection() cgiEnv(ENV_HTTP_CONNECTION)
#define cgiHttpHost() cgiEnv(ENV_HTTP_HOST)
#define cgiHttpUserAgent() cgiEnv(ENV_HTTP_USER_AGENT)
#define cgiPathInfo() cgiEnv(ENV_PATH_INFO)
#define cgiPathTranslated() cgiEnv(ENV_PATH_TRANSLATED)
#define cgiRemoteHost() cgiEnv(ENV_REMOTE_HOST)
#define cgiRemoteAddr() cgiEnv(ENV_REMOTE_ADDR)
#define cgiRemotePort() cgiEnv(ENV_REMOTE_PORT)
#define cgiRemoteUser() cgiEnv(ENV_REMOTE_USER)
#define cgiRemoteIdent() cgiEnv(ENV_REMOTE_IDENT)
#define cgiAuthType() cgiEnv(ENV_AUTH_TYPE)
#define cgiScriptName() cgiEnv(ENV_SCRIPT_NAME)
#define cgiScriptFilename() cgiEnv(ENV_SCRIPT_FILENAME)
#define cgiServerAddr() cgiEnv(ENV_SERVER_ADDR)
#define cgiServerAdmin() cgiEnv(ENV_SERVER_ADMIN)
#define cgiServerName() cgiEnv(ENV_SERVER_NAME)
#define cgiServerPort() cgiEnv(ENV_SERVER_PORT)
#define cgiServerProtocol() cgiEnv(ENV_SERVER_PROTOCOL)
#define cgiServerSignature() cgiEnv(ENV_SERVER_SIGNATURE)
#define cgiServerSoftware() cgiEnv(ENV_SERVER_SOFTWARE)


#endif

